/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.t.passport.controller  
 */
package com.apache.passport.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.action.PlatFromAction;
import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.CacheManager;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.passport.entity.BlackRoster;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;

/**
 * description:  缓存读取操作类,负责缓存的数据管理
 * @author Hou Dayu 创建时间：2015-1-29  
 * @Copyright(c)2014:
 */
@Controller
@RequestMapping({ "/passport/cache/" })
public class CacheAction extends PlatFromAction<BlackRoster> {

	private CacheManager cacheManager = SystemTools.getInstance().getCache("loginErrToken");
	private CacheManager userCacheManager = SystemTools.getInstance().getCache("loginToken");
	private final String CACHE_LIST = getPrefix() + "cache-list";
	private final String CACHE_USER_LIST = getPrefix() + "cache-user-list";

	/**
	 * description: 查看当前受限制用户
	 * @param request
	 * @param page
	 * @param rows
	 * @return  
	 * @author Hou Dayu 
	 * @update 2015-1-29
	 */
	@RequestMapping({ "list!data.action" })
	@ResponseBody
	public Object list(HttpServletRequest request, String page, String rows) {
		List<String> lst = cacheManager.getAllKeys();
		List<Object> listObj = cacheManager.getObjects(lst);
		int pageInt = NumberUtils.getInt(page, 1);//起始页
		int rowsInt = NumberUtils.getInt(rows, 15);//一页显示条数
		Map<String, Object> mapData = new HashMap<String, Object>();
		List<Object> flightPageList = new ArrayList<Object>();
		if (!Validator.isEmpty(listObj)) {
			int count = listObj.size();//总条数
			int pageCount = 0;//总页数
			if (count % rowsInt == 0) {
				pageCount = count / rowsInt;
			} else {
				pageCount = (count / rowsInt) + 1;
			}

			//超过总页数置为最后一页
			if (pageInt > pageCount) {
				pageInt = pageCount;
			}

			int tj1 = ((pageInt - 1) * rowsInt);
			for (int i = tj1; i < count && i < ((pageInt) * rowsInt) && pageInt > 0; i++) {
				flightPageList.add(listObj.get(i));
			}
			mapData.put("total", Integer.valueOf(listObj.size()));
		} else {
			mapData.put("total", 0);
		}

		mapData.put("rows", flightPageList);
		return mapData;
	}

	/**
	 * description:  当前登录用户查看
	 * @param request
	 * @param page
	 * @param rows
	 * @return  
	 * @author Hou Dayu 
	 * @update 2015-1-30
	 */
	@RequestMapping({ "list!userdata.action" })
	@ResponseBody
	public Object userlist(HttpServletRequest request, HttpServletResponse response, String page, String rows) {
		List<String> lst = userCacheManager.getAllKeys();
		List<Object> listObj = userCacheManager.getObjects(lst);
		int pageInt = NumberUtils.getInt(page, 1);//起始页
		int rowsInt = NumberUtils.getInt(rows, 15);//一页显示条数
		Map<String, Object> mapData = new HashMap<String, Object>();
		List<Object> flightPageList = new ArrayList<Object>();
		if (!Validator.isEmpty(listObj)) {
			int count = listObj.size();//总条数
			int pageCount = 0;//总页数
			if (count % rowsInt == 0) {
				pageCount = count / rowsInt;
			} else {
				pageCount = (count / rowsInt) + 1;
			}

			//超过总页数置为最后一页
			if (pageInt > pageCount) {
				pageInt = pageCount;
			}

			int tj1 = ((pageInt - 1) * rowsInt);
			for (int i = tj1; i < count && i < ((pageInt) * rowsInt) && pageInt > 0; i++) {
				flightPageList.add(listObj.get(i));
			}
			mapData.put("total", Integer.valueOf(listObj.size()));
		} else {
			mapData.put("total", 0);
		}
		mapData.put("rows", flightPageList);
		return mapData;
	}

	/**
	 * TODO 从缓存中删除锁定用户.  
	 * @see com.apache.t.action.PlatFromAction#delete(javax.servlet.http.HttpServletRequest, java.lang.String)
	 */
	@RequestMapping({ "del.action" })
	@ResponseBody
	public ResultMsg delete(HttpServletRequest request, String id) {
		ResultMsg rmsg = new ResultMsg("F", "操作失败,请重试!");
		try {
			if (StrUtil.isNotNull(id)) {
				boolean flag = cacheManager.removeCacheObject(id);
				if (flag) {
					rmsg.setFlag("T");
					rmsg.setMsg("删除成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rmsg;
	}

	/**
	 * description:  强制退出登录用户<慎用>
	 * @param request
	 * @param id
	 * @return  
	 * @author Hou Dayu 
	 * @update 2015-1-30
	 */
	@RequestMapping({ "del-user.action" })
	@ResponseBody
	public ResultMsg deleteUser(HttpServletRequest request, String id) {
		ResultMsg rmsg = new ResultMsg("F", "操作失败,请重试!");
		try {
			if (StrUtil.isNotNull(id)) {
				boolean flag = userCacheManager.removeCacheObject(id);
				if (flag) {
					rmsg.setFlag("T");
					rmsg.setMsg("删除成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rmsg;
	}

	/**
	 * description: 受限制用户页面跳转
	 * @param request
	 * @return  
	 * @author Hou Dayu 
	 * @update 2015-1-29
	 */
	@RequestMapping({ "cache-list.action" })
	public ModelAndView cachelist(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(CACHE_LIST);
		return view;
	}

	@RequestMapping({ "cache-user-list.action" })
	public ModelAndView cacheuserlist(HttpServletRequest request) {
		ModelAndView view = new ModelAndView(CACHE_USER_LIST);
		return view;
	}

	@Override
	protected Object data(HttpServletRequest arg0, String arg1, String arg2) {
		return null;
	}

	@Override
	protected BaseManager<BlackRoster> getBaseManager() {
		return null;
	}

	@Override
	protected String getPrefix() {
		return "/passport/passport-";
	}

}
